package com.hs.conc.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: songhao
 * @Date: 2021/8/28
 * @Description:
 *  主线程获取子线程的值
 *  方法：创建一个线程池，通过callable获取结果
 */
public class MyThread9 implements Callable<Integer> {

    public static void main(String[] args) throws Exception{
        long start=System.currentTimeMillis();

        //创建一个线程的线程池
        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
        MyThread9 myThread2 = new MyThread9();
        Future<Integer> res = executor.submit(myThread2);//异步提交
        //关闭线程池
        executor.shutdown();

        // 确保  拿到result 并输出
        System.out.println("计算结果为：" + res.get());
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }

    public Integer call() throws Exception {
        return sum();
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
